package com.mtiiot.fstcpserver.fastdfs.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class UploadStream
  implements UploadCallback
{
  private InputStream inputStream;
  private long fileSize = 0L;

  public UploadStream(InputStream inputStream, long fileSize)
  {
    this.inputStream = inputStream;
    this.fileSize = fileSize;
  }

  public int send(OutputStream out)
    throws IOException
  {
    long remainBytes = this.fileSize;
    byte[] buff = new byte[262144];
    int bytes;
    while (remainBytes > 0L)
    {
      try
      {
        if ((bytes = this.inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int)remainBytes)) < 0)
        {
          return -1;
        }
      }
      catch (IOException ex)
      {
        ex.printStackTrace();
        return -1;
      }
      out.write(buff, 0, bytes);
      remainBytes -= bytes;
    }
    return 0;
  }
}